﻿using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;

namespace CAPDemo1.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class PublishController:ControllerBase
    {
        [HttpGet("send")]
        public IActionResult SendMessage([FromServices] ICapPublisher capBus)
        {
            var header = new Dictionary<string, string>()
            {
                ["my.header.first"] = "first",
                ["my.header.second"] = "second"
            };
            capBus.Publish("test.show.time", DateTime.Now,headers: header!);

            return Ok();
        }

        [HttpGet("sendDelayMessage")]
        public IActionResult SendDelayMessage([FromServices] ICapPublisher capBus)
        {
            capBus.PublishDelay(TimeSpan.FromSeconds(100), "test.show.time", DateTime.Now);

            return Ok();
        }

        [HttpGet("sendDelayMessageWithHeader")]
        public IActionResult SendDelayMessageWithHeader([FromServices] ICapPublisher capBus)
        {

            capBus.PublishDelay(TimeSpan.FromSeconds(100), "test.show.time", DateTime.Now);

            return Ok();
        }

        [NonAction]
        [CapSubscribe("test.show.time")]
        public void ReceiveMessage(DateTime time, [FromCap] CapHeader header)
        {
            Console.WriteLine("message time is:" + time);
        }
    }
}
